/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lu.med.praxiswahl.wahlumed.service.beans;

import javax.ejb.EJB;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import lu.med.praxiswahl.wahlumed.dao.beans.daointerface.CustomerDAOInt;
import lu.med.praxiswahl.wahlumed.model.beans.CustomerTBL;
import lu.med.praxiswahl.wahlumed.model.beans.DoctorsTBL;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.CustomerServiceInt;

/**
 * 
 * @author pkendzo
 */
@Stateless
@Remote({ CustomerServiceInt.class })
public class CustomerService implements CustomerServiceInt {

	@EJB
	private CustomerDAOInt custDao;
	private CustomerTBL tmpCust;

	// private ConcurrencyBufferService concurrent;

	public CustomerService() {

		tmpCust = new CustomerTBL();
		// concurrent = new ConcurrencyBufferService();

	}

	/* #################################################### */
	public boolean isMatriculeAvailable(String matricule) {
		return custDao.RegisterNumberAvailable(matricule);
	}

	/* ############ getter Methoden ############ */

	public String getArbeitgeber(String matricule) {
		// return
		// custDao.findCustomerByRegisterNumber(matricule).getArbeitgeber();
		return tmpCust.getArbeitgeber();

	}

	public String getArbeitgeberTel(String matricule) {
		// return
		// custDao.findCustomerByRegisterNumber(matricule).getArbeitgeberTel();
		return tmpCust.getArbeitgeberTel();
	}

	public String getBesonderheiten(String matricule) {
		// return
		// custDao.findCustomerByRegisterNumber(matricule).getBesonderheiten();
		return tmpCust.getBesonderheiten();
	}

	public String getGeburtsdatum(String matricule) {
		// return
		// custDao.findCustomerByRegisterNumber(matricule).getGeburtstag();
		return tmpCust.getGeburtstag();
	}

	public String[] getNachname(String matricule) {
		String[] tmp = new String[3];

		/*
		 * tmp[0] =
		 * custDao.findCustomerByRegisterNumber(matricule).getNachname_1();
		 * tmp[1] =
		 * custDao.findCustomerByRegisterNumber(matricule).getNachname_2();
		 * tmp[2] =
		 * custDao.findCustomerByRegisterNumber(matricule).getNachname_3();
		 */

		if (tmpCust == null) {
			tmp[0] = "";
			tmp[1] = "";
			tmp[2] = "";

		} else {
			tmp[0] = tmpCust.getNachname_1();
			tmp[1] = tmpCust.getNachname_2();
			tmp[2] = tmpCust.getNachname_3();

		}

		return tmp;
	}

	public String getNotizen(String matricule) {
		// return custDao.findCustomerByRegisterNumber(matricule).getNotizen();
		return tmpCust.getNotizen();
	}

	public String getPrivatAnschrift(String matricule) {
		try {
			// return
			// custDao.findCustomerByRegisterNumber(matricule).getAddresse();
			return tmpCust.getAddresse();
		} catch (Exception e) {
			throw new NullPointerException("no customer with this matricule");
		}
	}

	public String getPrivatEMail(String matricule) {
		// return custDao.findCustomerByRegisterNumber(matricule).getEmail();
		return tmpCust.getEmail();
	}

	public String getPrivatFax(String matricule) {
		// return custDao.findCustomerByRegisterNumber(matricule).getFax();
		return tmpCust.getFax();
	}

	public String getPrivatGSM(String matricule) {
		// return
		// custDao.findCustomerByRegisterNumber(matricule).getPrivatGSM();
		return tmpCust.getPrivatGSM();
	}

	public String getPrivatOrt(String matricule) {
		// return custDao.findCustomerByRegisterNumber(matricule).getOrt();
		return tmpCust.getOrt();
	}

	public String getPrivatPLZ(String matricule) {
		// return
		// custDao.findCustomerByRegisterNumber(matricule).getPrivatPLZ();
		return tmpCust.getPrivatPLZ();
	}

	public String getPrivatTel(String matricule) {
		// return custDao.findCustomerByRegisterNumber(matricule).getTelefon();
		return tmpCust.getTelefon();
	}

	public String getVorname(String matricule) {
		// return custDao.findCustomerByRegisterNumber(matricule).getVorname();
		return tmpCust.getVorname();
	}

	public Long getHausArtz(String matricule) {
		// wird ergänzt
		// return
		// custDao.findCustomerByRegisterNumber(matricule).getHaus_artzID().getNachname();
		if (tmpCust.getHaus_artzID() != null)
			return tmpCust.getHaus_artzID().getId();
		return null;
	}

	public String getWebsite(String matricule) {
		// return custDao.findCustomerByRegisterNumber(matricule).getWebsite();
		return tmpCust.getWebsite();
	}

	public String getAlteAdresse(String matricule) {
		// return
		// custDao.findCustomerByRegisterNumber(matricule).getOldAddresse();
		return tmpCust.getOldAddresse();
	}

	public String[] getMatriculeOfName(String suchStr) {
		Object[] tmp = custDao.findRegisterNumberBySubStringLastName(suchStr)
				.toArray();
		String[] matricule = new String[tmp.length];
		CustomerTBL localTmpCust = new CustomerTBL();

		for (int i = 0; i < tmp.length; i++) {
			localTmpCust = (CustomerTBL) tmp[i]; // Temporäre Patientin

			matricule[i] = localTmpCust.getMatrikel_nummer();
			localTmpCust = null;
		}
		return matricule;
	}

	public String[] getMatrciuleOfMatriculePart(String suchStr) {
		Object[] tmp = custDao.findRegisterNumberBySubStringMatricule(suchStr)
				.toArray();
		String[] matricule = new String[tmp.length];
		CustomerTBL localTmpCust = new CustomerTBL();

		for (int i = 0; i < tmp.length; i++) {
			localTmpCust = (CustomerTBL) tmp[i]; // Temporäre Patientin

			matricule[i] = localTmpCust.getMatrikel_nummer();
			localTmpCust = null;
		}
		return matricule;
	}

	public String[] getMatriculeOfVorname(String suchStr) {
		Object[] tmp = custDao.findRegisterNumberBySubStringFirstName(suchStr)
				.toArray();
		String[] matricule = new String[tmp.length];
		CustomerTBL localTmpCust = new CustomerTBL();

		for (int i = 0; i < tmp.length; i++) {
			localTmpCust = (CustomerTBL) tmp[i]; // Temporäre Patientin

			matricule[i] = localTmpCust.getMatrikel_nummer();
			localTmpCust = null;
		}
		return matricule;
	}

	public String getSprache(String matricule) {
		// return custDao.findCustomerByRegisterNumber(matricule).getSprache();
		return tmpCust.getSprache();
	}

	public String getCustomerMatricule() {
		return tmpCust.getMatrikel_nummer();
	}

	public CustomerTBL getCustomer(String matricule) {
		tmpCust = custDao.findCustomerByRegisterNumber(matricule);
		return tmpCust;
	}

	public String[] getNachnameEhemann(String matricule) {
		String[] tmp = new String[3];

		/*
		 * tmp[0] =
		 * custDao.findCustomerByRegisterNumber(matricule).getEhemann_nachname_1
		 * (); tmp[1] =
		 * custDao.findCustomerByRegisterNumber(matricule).getEhemann_nachname_2
		 * (); tmp[2] =
		 * custDao.findCustomerByRegisterNumber(matricule).getEhemann_nachname_3
		 * ();
		 */

		tmp[0] = tmpCust.getEhemann_nachname_1();
		tmp[1] = tmpCust.getEhemann_nachname_2();
		tmp[2] = tmpCust.getEhemann_nachname_3();
		return tmp;
	}

	public String getVornameEhemann(String matricule) {
		// return
		// custDao.findCustomerByRegisterNumber(matricule).getEhemann_vorname();
		return tmpCust.getEhemann_vorname();
	}

	public String getGeburtstagEhemann(String matricule) {
		// return
		// custDao.findCustomerByRegisterNumber(matricule).getEhemann_geburtstag();
		return tmpCust.getEhemann_geburtstag();
	}

	public String[] getAllMatricules() {
		Object[] tmp = custDao.findAvailableCustomers().toArray();
		String[] matricules = new String[tmp.length];
		CustomerTBL localTmpCust = new CustomerTBL();

		for (int i = 0; i < tmp.length; i++) {
			localTmpCust = (CustomerTBL) tmp[i]; // Temporäre Patientin

			matricules[i] = localTmpCust.getMatrikel_nummer();
			localTmpCust = null;
		}
		return matricules;
	}

	public String getGeheimNummer(String matricule) {
		// return
		// custDao.findCustomerByRegisterNumber(matricule).getGeheim_nummer();
		return tmpCust.getGeheim_nummer();
	}

	public String getVersicherung_Auswahl(String matricule) {
		// return
		// custDao.findCustomerByRegisterNumber(matricule).getVersichert_auswahl();
		return tmpCust.getVersichert_auswahl();
	}

	/*********************************************************
	 *********************** 
	 Anderungen, neue Einträge erfolgen mit folgenden Methoden
	 *********************** 
	 * ********************************************************
	 */
	public int createCustomer(String matricule) {
		tmpCust = null;
		tmpCust = new CustomerTBL();
		if (this.isMatriculeAvailable(matricule)) {
			tmpCust.setMatrikel_nummer(matricule);
			custDao.persist(tmpCust);

			return RET_OPERATION_DONE_PROPERLY;
		} else {
			System.out.println("This matricule number already exist");

			return RET_OPERATION_FAILED;

		}

	}

	public void setVersicherung_traeger_Of(String durch) {
		tmpCust.setVersichern_duch(durch);
	}

	public void setCustomerMatricule(String matricule) {
		tmpCust.setMatrikel_nummer(matricule);
	}

	public void setSprache(String sprache) {
		tmpCust.setSprache(sprache);
	}

	public void setArbeitgeber(String arbeitgeber) {
		tmpCust.setArbeitgeber(arbeitgeber);
	}

	public void setArbeitgeberTel(String arbeitgeberTel) {
		tmpCust.setArbeitgeberTel(arbeitgeberTel);
	}

	public void setBesonderheiten(String besonderheiten) {
		tmpCust.setBesonderheiten(besonderheiten);
	}

	public void setGeburtsdatum(String geburtsdatum) {
		tmpCust.setGeburtstag(geburtsdatum);
	}

	public void setNachname(String[] nachname) {
		tmpCust.setNachname_1(nachname[0]);
		tmpCust.setNachname_2(nachname[1]);
		tmpCust.setNachname_3(nachname[2]);

	}

	public void setNotizen(String notizen) {
		tmpCust.setNotizen(notizen);

	}

	public void setPrivatAnschrift(String privatAnschrift) {
		String tmpAnschrift = this.getPrivatAnschrift(this
				.getCustomerMatricule());

		if (tmpAnschrift == null) {
			tmpCust.setAddresse(privatAnschrift);
		} else {
			tmpAnschrift += " -- ("
					+ this.getPrivatPLZ(this.getCustomerMatricule()) + ")";
			this.setAlteAdresse(tmpAnschrift);

			tmpCust.setAddresse(privatAnschrift);
		}
	}

	public void setPrivatEMail(String privatEMail) {
		tmpCust.setEmail(privatEMail);
	}

	public void setPrivatFax(String privatFax) {
		tmpCust.setFax(privatFax);
	}

	public void setPrivatGSM(String privatGSM) {
		tmpCust.setPrivatGSM(privatGSM);
	}

	public void setPrivatOrt(String privatOrt) {
		tmpCust.setOrt(privatOrt);
	}

	public void setPrivatPLZ(String privatPLZ) {
		tmpCust.setPrivatPLZ(privatPLZ);
	}

	public void setPrivatTel(String privatTel) {
		tmpCust.setTelefon(privatTel);
	}

	public void setVorname(String vorname) {
		tmpCust.setVorname(vorname);
	}

	public String getVersicherung_traeger_Of(String matricule) {
		return custDao.findCustomerByRegisterNumber(matricule)
				.getVersichern_duch();
	}

	public void setWebsite(String val) {
		tmpCust.setWebsite(val);
	}

	public void setAlteAdresse(String val) {
		tmpCust.setOldAddresse(val);
	}

	// Vorsicht !!

	public int changeMatricule(String from, String to) {
		custDao.updateMatricule(from, to);

		// Eine Überprüfung wird an diese Stelle durchgefürht.

		/*
		 * if (!custDao.findCustomerByRegisterNumber(to).equals(null)) { return
		 * CustomerServiceInt.RET_OPERATION_DONE_PROPERLY; } return
		 * CustomerServiceInt.RET_OPERATION_FAILED;
		 */

		return RET_OPERATION_DONE_PROPERLY;
	}

	public int deletePatient(String matricule) {
		// tmpCust = custDao.findCustomerByRegisterNumber(matricule);

		custDao.delete(matricule);

		// Eine Überprüfung wird an diese Stelle durchgefürht.

		// if (tmpCust.equals(null))
		// {
		// return CustomerServiceInt.RET_OPERATION_DONE_PROPERLY;
		// }
		// return CustomerServiceInt.RET_OPERATION_FAILED;
		return RET_OPERATION_DONE_PROPERLY;
	}

	public void setVornameEhemann(String vornameEhemann) {
		tmpCust.setEhemann_vorname(vornameEhemann);
	}

	public void setNachnameEhemann(String[] nachname) {
		tmpCust.setEhemann_nachname_1(nachname[0]);
		tmpCust.setEhemann_nachname_2(nachname[1]);
		tmpCust.setEhemann_nachname_3(nachname[2]);
	}

	public void setGeburtstagEhemann(String geburtstag) {
		tmpCust.setEhemann_geburtstag(geburtstag);
	}

	public void setHausArtz(DoctorsTBL hausartz) {
		// wird ergänzt
		tmpCust.setHaus_artzID(hausartz);
	}

	public void setGeheimNummer(String geheimnummer) {
		tmpCust.setGeheim_nummer(geheimnummer);
	}

	public void setVersicherung_Auswahl(String versichertAuswahl) {
		tmpCust.setVersichert_auswahl(versichertAuswahl);
	}

	public void save_customer() {
		try {
			if (this.isMatriculeAvailable(this.getCustomerMatricule())) {
				custDao.persist(tmpCust);

			} else {
				custDao.update(tmpCust);

			}
		} catch (Exception e) {
			throw new IllegalArgumentException("wrong target");
		}

	}

	@Override
	public CustomerTBL[] getAllCustomer() {
		Object[] tmp = custDao.findAvailableCustomers().toArray();
		CustomerTBL[] eintraege = new CustomerTBL[tmp.length];

		for (int i = 0; i < tmp.length; i++) {
			eintraege[i] = (CustomerTBL) tmp[i]; // Temporäre Patientin

		}
		return eintraege;
	}

	@Override
	public String getZusatzVersicherung() {
		return tmpCust.getZusatz_versicherung();
	}

	@Override
	public void setZusatzVersicherung(String zusatz_versicherung) {
		tmpCust.setZusatz_versicherung(zusatz_versicherung);

	}

	@Override
	public CustomerTBL[] getCustomerByFirstNameAndLastName(String firstName,
			String lastName) {
		Object[] tmp = custDao.findCustomerByFirstNameAndLastName(firstName,
				lastName).toArray();
		CustomerTBL[] eintraege = new CustomerTBL[tmp.length];

		for (int i = 0; i < tmp.length; i++) {
			eintraege[i] = (CustomerTBL) tmp[i]; // Temporäre Patientin

		}
		return eintraege;
	}

	@Override
	public CustomerTBL[] getCustomerByFirstNameAndLastNameAndMatricule(
			String firstName, String lastName, String matricule) {
		Object[] tmp = custDao.findCustomerByFirstNameAndLastNameAndMatricule(
				firstName, lastName, matricule).toArray();
		CustomerTBL[] eintraege = new CustomerTBL[tmp.length];

		for (int i = 0; i < tmp.length; i++) {
			eintraege[i] = (CustomerTBL) tmp[i]; // Temporäre Patientin

		}
		return eintraege;
	}

	@Override
	public CustomerTBL[] getCustomerByFirstNameAndMatricule(String firstName,
			String matricule) {
		Object[] tmp = custDao.findCustomerByFirstNameAndMatricule(firstName,
				matricule).toArray();
		CustomerTBL[] eintraege = new CustomerTBL[tmp.length];

		for (int i = 0; i < tmp.length; i++) {
			eintraege[i] = (CustomerTBL) tmp[i]; // Temporäre Patientin

		}
		return eintraege;
	}

	@Override
	public CustomerTBL[] getCustomerByLastNameAndMatricule(String lastName,
			String matricule) {
		Object[] tmp = custDao.findCustomerByLastNameAndMatricule(lastName,
				matricule).toArray();
		CustomerTBL[] eintraege = new CustomerTBL[tmp.length];

		for (int i = 0; i < tmp.length; i++) {
			eintraege[i] = (CustomerTBL) tmp[i]; // Temporäre Patientin

		}
		return eintraege;
	}

	@Override
	public String getKrankMeldung(String matricule) {
		return tmpCust.getKrankmeldung_feld();
	}

	@Override
	public void setKrankMeldung(String krankMeldungText) {
		tmpCust.setKrankmeldung_feld(krankMeldungText);
	}

	@Override
	public CustomerTBL[] retrieveCustomer(String searchStr) {
		Object[] tmp = custDao.retrieveCustomer(searchStr).toArray();
		CustomerTBL[] eintraege = new CustomerTBL[tmp.length];

		for (int i = 0; i < tmp.length; i++) {
			eintraege[i] = (CustomerTBL) tmp[i]; // Temporäre Patientin

		}
		return eintraege;
	}
}
